<?php
namespace frontend\controllers;


use common\models\Chat;
use Yii;
use yii\helpers\Url;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use dosamigos\qrcode\QrCode;
/**
 * Site controller
 */
class AskController extends Controller
{
    /**
     * @inheritdoc
     */
    public $layout='meno.php';
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],

            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],

        ];
    }

    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }
    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        $this->layout="wide.php";
        if(isset($_POST['more'])) echo json_encode("ok");
        return $this->render('index');
    }



    public function actionAsk($question){

        $uid=Yii::$app->user->id;
        $record=Chat::find()->where(['userId'=>$uid,'status'=>1])->orderBy('created_at DESC')->one();
        if(isset($record)&&(time()-$record->created_at<60)){
            echo "asked";
        }else {

            $model = new Chat();
            $model->userId = "$uid";
            $model->content = $question;
            $model->parent_id = 0;
            $model->to = 0;
            $model->status = 1;
            $model->username = Yii::$app->user->identity->username;
            if ($model->save())
            {
                echo "yes";
            } else   {
                echo "no";
            }
        }

    }

}
